package cn.fl.mcr.controller;


import cmm.mid.core.framework.page.DataResultVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.mcr.service.IMcrBillVerifyInfoService;
import cn.lionbridgecapital.loan.model.utils.CollectionUtils;
import cn.lionbridgecapital.loan.model.utils.StringUtils;
import cn.lionbridgecapital.loan.system.constant.SysLogConstant;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


/**
 * @Description: 核销明细表 控制层
 */
@Controller
@RequestMapping("/mcr/mcrBillVerify")
public class McrBillVerifyInfoController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(McrBillVerifyInfoController.class);

	/**
	 * McrBillVerifyInfo 服务层BEAN
	 */
	@Autowired
	private IMcrBillVerifyInfoService mcrBillVerifyInfoService;

	/**
	 * 到列表页面
	 */
	@RequestMapping(value = "/toListPage.do", method = { RequestMethod.GET})
	public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
		Long startTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toListPage", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
		ModelAndView view = new ModelAndView("mcr/mcrBillVerify/mcrBillVerifyList");
		Long endTm = System.currentTimeMillis();
		BaseLogUtils.info(logger, "toListPage", "加载列表页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return view;
	}

    /**
     * 手动核销
     * @param request
     * @param response
     * @return
     */
	@RequestMapping(value = "/execVerifyForBill.do")
    @ResponseBody
	public Object execVerifyForBill(HttpServletRequest request, HttpServletResponse response) throws BaseException {

		//手工核销集合列表
		List<Long> billVerifyIds = new ArrayList<>();
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		try {
			//手工核销列表
			String billVerifyIdArr[] = request.getParameterValues("billVerifyIds[]");
			BaseLogUtils.info(logger, "execVerifyForBill", "手动执行核销请求.请求参数：" + JSON.toJSONString(billVerifyIdArr), CurrentThreadContext.getCurrentUserName());
			if ((null == billVerifyIdArr) || (null != billVerifyIdArr && billVerifyIdArr.length == 0)) {
				result.setInfo("手动核销执行失败，缺少必要入参");
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				return result;
			}
			for (String billVerifyId : billVerifyIdArr) {
				billVerifyIds.add(Long.valueOf(billVerifyId));
			}
			BaseLogUtils.info(logger, "execVerifyForBill", "手动执行核销请求.请求参数List：" + JSON.toJSONString(billVerifyIds), CurrentThreadContext.getCurrentUserName());
			if (CollectionUtils.isNotEmpty(billVerifyIds)) {
				mcrBillVerifyInfoService.execVerifyForBill(billVerifyIds);
				result.setInfo("手动核销执行成功！");
				result.setSuccess(BaseConstant.IS_YESNO_YES);
			} else {
				result.setInfo("手动核销执行失败，缺少必要入参");
				result.setSuccess(BaseConstant.IS_YESNO_NO);
			}
		} catch (BaseException e) {
			BaseLogUtils.error(logger, "execVerifyForBill", "手动核销执行异常",e);
			result.setInfo("手动核销执行异常");
			result.setSuccess(BaseConstant.IS_YESNO_NO);
		}catch (Exception e) {
			BaseLogUtils.error(logger, "execVerifyForBill", "手动核销执行异常",e);
			result.setInfo("手动核销执行异常");
			result.setSuccess(BaseConstant.IS_YESNO_NO);
		}
		return result;
	}


	@RequestMapping(value = "/verifySynState.do")
    @ResponseBody
	public Object verifySynState(HttpServletRequest request, HttpServletResponse response){
        DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
        //资方类型
        String loanChannel = request.getParameter("loanChannel");
		BaseLogUtils.info(logger, "execVerifyForBill", "手动执行核销请求.请求参数：" + loanChannel, CurrentThreadContext.getCurrentUserName());
		try {
			if(StringUtils.isNotEmpty(loanChannel)){
				mcrBillVerifyInfoService.synBillVerifyState(loanChannel);
				result.setSuccess(BaseConstant.IS_YESNO_YES);
				result.setInfo("核销结果同步成功！");
			}else{
				result.setSuccess(BaseConstant.IS_YESNO_NO);
				result.setInfo("核销结果同步失败，缺少必要入参");
			}
		} catch (BaseException e) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("核销结果同步异常！");
		}
		return result;
	}

	/**
	 * 初始化列表
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/initList.do")
	@ResponseBody
	public Object initList(HttpServletRequest request, HttpServletResponse response) {
//		Long startTm = System.currentTimeMillis();
//		BaseLogUtils.info(logger, "billVerifyInfoInitList", "加载列表页面.请求参数：" + this.getRequestParameter(request), CurrentThreadContext.getCurrentUserName());
//		String currentPage = request.getParameter("page");
//		String pageSize = request.getParameter("pagesize");
//		String pushType = request.getParameter("pushType");
//		String payDt = request.getParameter("payDt");
//		String dataType = request.getParameter("dataType");
//		String state = request.getParameter("state");
//		String loanChannel = request.getParameter("loanChannel");
//		String crCntNo = request.getParameter("crCntNo");
//		Pager page = new Pager();
//		if (StringUtils.isNotEmpty(currentPage)) {
//			page.setCurrentPage(Integer.parseInt(currentPage));
//		}
//
//		if (StringUtils.isNotEmpty(pageSize)) {
//			page.setEveryPage(Integer.parseInt(pageSize));
//		}
//
//		McrBillVerifyInfoQC qc = new McrBillVerifyInfoQC();
//		if (StringUtils.isNotEmpty(pushType)) {
//			qc.setPushType(pushType);
//		}
//		if (StringUtils.isNotEmpty(crCntNo)) {
//			qc.setCrCntNo(crCntNo);
//		}
//		if (StringUtils.isNotEmpty(loanChannel)) {
//			qc.setLoanChannel(loanChannel);
//		}
//		if (StringUtils.isNotEmpty(state)) {
//			qc.setLoanChannel(state);
//		}
//		if (StringUtils.isNotEmpty(payDt)) {
//			qc.setPayDt(DateUtils.formatStringToDate(payDt,DateUtils.YYYY_MM_DD));
//		}
//		page.setCondition(qc);
//		BaseLogUtils.info(logger, "initList", "核销列表页.请求参数：" + JSON.toJSONString(page));
//		//TODO SOMETHING
//		Pager pager = this.mcrBillVerifyInfoService.searchListPage(page);
//		List<McrBillVerifyInfoVO> list = (List<McrBillVerifyInfoVO>) pager.getResultList();
//		ListPageVO<McrBillVerifyInfoVO> listPage = new ListPageVO<McrBillVerifyInfoVO>(list, pager.getTotalCount());
//		Long endTm = System.currentTimeMillis();
//		BaseLogUtils.info(logger, "initList", "加载列表页面.耗时：" + (endTm-startTm), CurrentThreadContext.getCurrentUserName());
		return null;
	}



}

